package com.cro.prison.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ErrorBehaviorMethod {
    /**
     * 将查询的设置
     * 抽成公共方法
     * @param personId
     * @param date
     * @param dateAfter
     * @param type
     * @return
     */
    public static QueryWrapper getQueryByPersonIdAndDate(Long personId, Date date, Date dateAfter, Integer type){
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("create_time",date).lt("create_time",dateAfter).eq("abnormal_behavior_type",type).eq("person_id", personId);
        return queryWrapper;
    }

    /**
     * 获取前端需要的times
     * @param beginDate
     * @param endDate
     * @param periodBehaviorMap
     * @return
     */
    public static Integer[] getTimes(Date beginDate, Date endDate, Map<String, List<Object>> map){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
        List<Integer> list = new ArrayList<>();
        //从开始到结束每一天(默认前端传给后端的时间格式精确到天)
        for (long i = beginDate.getTime();i<endDate.getTime();i+=86400000){
            //将i进行格式转换
            Date currentDate = new Date(i);
            String currentDateString = simpleDateFormat.format(currentDate);
            list.add(map.get(currentDateString)!=null?map.get(currentDateString).size():0);
        }
        Integer[] result = new Integer[list.size()];
        int index = 0;
        for (Integer integer : list) {
            result[index] = integer;
            index++;
        }
        return result;
    }
}
